/*
 * File: Fu_SensAvrgFast.c
 *
 * Code generated for Simulink model 'Fu_SensAvrgFast'.
 *
 * Model version                  : 1.119
 * Simulink Coder version         : 8.2 (R2012a) 29-Dec-2011
 * TLC version                    : 8.2 (Dec 29 2011)
 * C/C++ source code generated on : Thu Jun 06 00:32:53 2013
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: ARM Compatible->ARM Cortex
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#include "Fu_SensAvrgFast.h"
#include "Fu_SensAvrgFast_private.h"

/* Exported block signals */
uint16_T Adc_RawCurrU;                 /* '<S2>/Divide1' */
uint16_T Adc_RawCurrV;                 /* '<S2>/Divide2' */
int16_T Net_CurrPhasV;                 /* '<S3>/Data Type Conversion' */
int16_T Net_CurrPhasU;                 /* '<S3>/Data Type Conversion1' */
int16_T Net_CurrPhasW;                 /* '<S3>/Add' */

/* Block signals (auto storage) */
BlockIO_Fu_SensAvrgFast Fu_SensAvrgFast_B;

/* Block states (auto storage) */
D_Work_Fu_SensAvrgFast Fu_SensAvrgFast_DWork;

/* Real-time model */
RT_MODEL_Fu_SensAvrgFast Fu_SensAvrgFast_M_;
RT_MODEL_Fu_SensAvrgFast *const Fu_SensAvrgFast_M = &Fu_SensAvrgFast_M_;

/* Model step function */
void Fu_SensAvrgFast_step(void)
{
  int32_T cff;
  int16_T acc;
  int32_T j;
  int16_T rtb_Product1;
  int16_T rtb_Product1_a;

  /* Product: '<S2>/Divide1' incorporates:
   *  Inport: '<Root>/In1'
   *  Sum: '<S2>/Add2'
   */
  Adc_RawCurrU = (uint16_T)((uint32_T)(uint16_T)(((uint32_T)Nw_AdcRawFast[0] +
    (uint32_T)Nw_AdcRawFast[2]) + (uint32_T)Nw_AdcRawFast[4]) / 3U);

  /* Sum: '<S5>/Subtract' incorporates:
   *  Memory: '<S5>/Memory'
   *  Product: '<S3>/Product1'
   *  Product: '<S3>/Product5'
   *  Product: '<S3>/Product6'
   *  Sum: '<S3>/Add3'
   */
  cff = (div_s16s32((int16_T)((int16_T)((uint32_T)(uint16_T)(((uint32_T)
              Adc_RawCurrU << 16) / 4095U) * 27853U >> 15) - 27853) << 7, 655) -
         (Fu_SensAvrgFast_DWork.Memory_PreviousInput << 2)) >> 2;
  if (cff > 32767) {
    cff = 32767;
  } else {
    if (cff < -32768) {
      cff = -32768;
    }
  }

  /* Product: '<S5>/Product1' incorporates:
   *  Sum: '<S5>/Subtract'
   */
  cff = div_s16s32((int16_T)cff << 11, -2525) * 625;
  rtb_Product1 = (int16_T)(((cff < 0 ? 511 : 0) + cff) >> 9);

  /* Sum: '<S5>/Subtract1' incorporates:
   *  Memory: '<S5>/Memory'
   */
  Fu_SensAvrgFast_B.Subtract1 = (int16_T)((int16_T)((int16_T)
    (Fu_SensAvrgFast_DWork.Memory_PreviousInput << 2) - (int16_T)(rtb_Product1 <<
    2)) >> 2);

  /* DiscreteFir: '<S3>/LowPass' */
  acc = (int16_T)(Fu_SensAvrgFast_B.Subtract1 * 3277 >> 13);
  cff = 1;
  for (j = Fu_SensAvrgFast_DWork.LowPass_circBuf; j < 2; j++) {
    acc += (int16_T)(Fu_SensAvrgFast_DWork.LowPass_states[j] *
                     Fu_SensAvrgFast_ConstP.pooled8[cff] >> 13);
    cff++;
  }

  for (j = 0; j < Fu_SensAvrgFast_DWork.LowPass_circBuf; j++) {
    acc += (int16_T)(Fu_SensAvrgFast_DWork.LowPass_states[j] *
                     Fu_SensAvrgFast_ConstP.pooled8[cff] >> 13);
    cff++;
  }

  /* DataTypeConversion: '<S3>/Data Type Conversion' incorporates:
   *  DiscreteFir: '<S3>/LowPass'
   */
  Net_CurrPhasV = (int16_T)(acc * 25 >> 5);

  /* Product: '<S2>/Divide2' incorporates:
   *  Inport: '<Root>/In1'
   *  Sum: '<S2>/Add3'
   */
  Adc_RawCurrV = (uint16_T)((uint32_T)(uint16_T)(((uint32_T)Nw_AdcRawFast[1] +
    (uint32_T)Nw_AdcRawFast[3]) + (uint32_T)Nw_AdcRawFast[5]) / 3U);

  /* Sum: '<S4>/Subtract' incorporates:
   *  Memory: '<S4>/Memory'
   *  Product: '<S3>/Product2'
   *  Product: '<S3>/Product3'
   *  Product: '<S3>/Product8'
   *  Sum: '<S3>/Add1'
   */
  cff = (div_s16s32((int16_T)((int16_T)((uint32_T)(uint16_T)(((uint32_T)
              Adc_RawCurrV << 16) / 4095U) * 27853U >> 15) - 27853) << 7, 655) -
         (Fu_SensAvrgFast_DWork.Memory_PreviousInput_d << 2)) >> 2;
  if (cff > 32767) {
    cff = 32767;
  } else {
    if (cff < -32768) {
      cff = -32768;
    }
  }

  /* Product: '<S4>/Product1' incorporates:
   *  Sum: '<S4>/Subtract'
   */
  cff = div_s16s32(cff << 11, -2525) * 625;
  rtb_Product1_a = (int16_T)(((cff < 0 ? 511 : 0) + cff) >> 9);

  /* Sum: '<S4>/Subtract1' incorporates:
   *  Memory: '<S4>/Memory'
   */
  Fu_SensAvrgFast_B.Subtract1_k = (int16_T)((int16_T)((int16_T)
    (Fu_SensAvrgFast_DWork.Memory_PreviousInput_d << 2) - (int16_T)
    (rtb_Product1_a << 2)) >> 2);

  /* DiscreteFir: '<S3>/LowPass1' */
  acc = (int16_T)(Fu_SensAvrgFast_B.Subtract1_k * 3277 >> 13);
  cff = 1;
  for (j = Fu_SensAvrgFast_DWork.LowPass1_circBuf; j < 2; j++) {
    acc += (int16_T)(Fu_SensAvrgFast_DWork.LowPass1_states[j] *
                     Fu_SensAvrgFast_ConstP.pooled8[cff] >> 13);
    cff++;
  }

  for (j = 0; j < Fu_SensAvrgFast_DWork.LowPass1_circBuf; j++) {
    acc += (int16_T)(Fu_SensAvrgFast_DWork.LowPass1_states[j] *
                     Fu_SensAvrgFast_ConstP.pooled8[cff] >> 13);
    cff++;
  }

  /* DataTypeConversion: '<S3>/Data Type Conversion1' incorporates:
   *  DiscreteFir: '<S3>/LowPass1'
   */
  Net_CurrPhasU = (int16_T)(acc * 25 >> 5);

  /* Sum: '<S3>/Add' */
  Net_CurrPhasW = (int16_T)((-(Net_CurrPhasV << 7) - (Net_CurrPhasU << 7)) >> 7);

  /* Update for Memory: '<S5>/Memory' */
  Fu_SensAvrgFast_DWork.Memory_PreviousInput = rtb_Product1;

  /* Update for DiscreteFir: '<S3>/LowPass' */
  Fu_SensAvrgFast_DWork.LowPass_circBuf--;
  if (Fu_SensAvrgFast_DWork.LowPass_circBuf < 0) {
    Fu_SensAvrgFast_DWork.LowPass_circBuf = 1;
  }

  Fu_SensAvrgFast_DWork.LowPass_states[Fu_SensAvrgFast_DWork.LowPass_circBuf] =
    Fu_SensAvrgFast_B.Subtract1;

  /* End of Update for DiscreteFir: '<S3>/LowPass' */

  /* Update for Memory: '<S4>/Memory' */
  Fu_SensAvrgFast_DWork.Memory_PreviousInput_d = rtb_Product1_a;

  /* Update for DiscreteFir: '<S3>/LowPass1' */
  Fu_SensAvrgFast_DWork.LowPass1_circBuf--;
  if (Fu_SensAvrgFast_DWork.LowPass1_circBuf < 0) {
    Fu_SensAvrgFast_DWork.LowPass1_circBuf = 1;
  }

  Fu_SensAvrgFast_DWork.LowPass1_states[Fu_SensAvrgFast_DWork.LowPass1_circBuf] =
    Fu_SensAvrgFast_B.Subtract1_k;

  /* End of Update for DiscreteFir: '<S3>/LowPass1' */
}

/* Model initialize function */
void Fu_SensAvrgFast_initialize(void)
{
  /* Registration code */

  /* initialize error status */
  rtmSetErrorStatus(Fu_SensAvrgFast_M, (NULL));

  /* block I/O */
  (void) memset(((void *) &Fu_SensAvrgFast_B), 0,
                sizeof(BlockIO_Fu_SensAvrgFast));

  /* exported global signals */
  Adc_RawCurrU = 0U;
  Adc_RawCurrV = 0U;
  Net_CurrPhasV = 0;
  Net_CurrPhasU = 0;
  Net_CurrPhasW = 0;

  /* states (dwork) */
  (void) memset((void *)&Fu_SensAvrgFast_DWork, 0,
                sizeof(D_Work_Fu_SensAvrgFast));

  /* InitializeConditions for DiscreteFir: '<S3>/LowPass' */
  Fu_SensAvrgFast_DWork.LowPass_circBuf = 0;
  Fu_SensAvrgFast_DWork.LowPass_states[0] = 0;
  Fu_SensAvrgFast_DWork.LowPass_states[1] = 0;

  /* InitializeConditions for DiscreteFir: '<S3>/LowPass1' */
  Fu_SensAvrgFast_DWork.LowPass1_circBuf = 0;
  Fu_SensAvrgFast_DWork.LowPass1_states[0] = 0;
  Fu_SensAvrgFast_DWork.LowPass1_states[1] = 0;
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
